﻿/*
 * Copyright 2014 Francois Karman
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using Common.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;
using System.Threading;

namespace Quartz.WebManager.SimpleLog
{
    /// <summary>
    /// Tests the <see cref="FileLogger"/> class.
    /// </summary>
    [TestClass]
    public class FileLoggerTest
    {
        /// <summary>
        /// Initializes and clean-up tests by removing existing log files.
        /// </summary>
        [TestInitialize]
        [TestCleanup]
        public void TestInitialize()
        {
            Directory.GetFiles(Directory.GetCurrentDirectory(), "test*.log").ToList().ForEach(f => File.Delete(f));
        }

        /// <summary>
        /// Tests the constructor of the <see cref="FileLogger"/> class.
        /// </summary>
        [TestMethod]
        public void Constructor_Simple()
        {
            FileLogger instance = new FileLogger("test log", Common.Logging.LogLevel.Warn, true, true, true, null, "test.log");
            Assert.IsFalse(instance.IsFormat);
            Assert.AreEqual("test.log", instance.FileName);
        }

        /// <summary>
        /// Tests the constructor of the <see cref="FileLogger"/> class.
        /// </summary>
        [TestMethod]
        public void Constructor_Format()
        {
            FileLogger instance = new FileLogger("test log", Common.Logging.LogLevel.Warn, true, true, true, null, "test_{0:hhmmss}.log");
            Assert.IsTrue(instance.IsFormat);
            Assert.AreEqual("test_{0:hhmmss}.log", instance.FileName);
        }

        /// <summary>
        /// Tests the <see cref="FileLogger.WriteInternal"/> method.
        /// </summary>
        [TestMethod]
        public void WriteInternal_Simple()
        {
            FileLogger instance = new FileLogger("test log", Common.Logging.LogLevel.Warn, true, false, true, null, "test.log");
            instance.Warn("warn message");
            instance.Info("info message");
            instance.Fatal("fatal message");

            Assert.IsTrue(File.Exists("test.log"));
            string[] lines = File.ReadAllLines("test.log");
            Assert.AreEqual(2, lines.Length);
            Assert.AreEqual("[WARN]  test log - warn message", lines[0]);
            Assert.AreEqual("[FATAL] test log - fatal message", lines[1]);
        }

        /// <summary>
        /// Tests the <see cref="FileLogger.WriteInternal"/> method.
        /// </summary>
        [TestMethod]
        public void WriteInternal_Format()
        {
            FileLogger instance = new FileLogger("test log", Common.Logging.LogLevel.Warn, true, false, true, null, "test_{0:hhmmss}.log");
            instance.Warn("warn message");
            Thread.Sleep(1100);
            instance.Fatal("fatal message");

            string[] logFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "test*.log").OrderBy(f => f).ToArray();
            Assert.IsNotNull(logFiles);
            Assert.AreEqual(2, logFiles.Length);
            Assert.AreEqual("[WARN]  test log - warn message\r\n", File.ReadAllText(logFiles[0]));
            Assert.AreEqual("[FATAL] test log - fatal message\r\n", File.ReadAllText(logFiles[1]));
        }

        /// <summary>
        /// Tests the <see cref="FileLogger"/> class loaded from the config file.
        /// </summary>
        [TestMethod]
        public void FromAppConfig()
        {
            ILog instance = LogManager.GetLogger("test log");
            instance.Warn("warn message");
            Thread.Sleep(1100);
            instance.Fatal("fatal message");

            string[] logFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "test*.log").OrderBy(f => f).ToArray();
            Assert.IsNotNull(logFiles);
            Assert.AreEqual(2, logFiles.Length);
            Assert.AreEqual("[WARN]  test log - warn message\r\n", File.ReadAllText(logFiles[0]));
            Assert.AreEqual("[FATAL] test log - fatal message\r\n", File.ReadAllText(logFiles[1]));
        }
    }
}
